package com.qishon.pc.domain.util;

import com.qishon.pc.domain.model.MasPartAssemply;
import com.qishon.pc.domain.model.MasPartFabric;
import com.qishon.pc.domain.model.MasPartHd;
import com.qishon.pc.domain.model.MasPartRegion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kenwstar on 2017/1/7.
 */
@Component
public class PreViewUtils {
    /*
     * 获取 MasPartHd
     */
    public MasPartHd getMasPartHd(String filepath,String imageType) {
        File file = new File(filepath);
        File[] listfile = file.listFiles();
        MasPartHd masPartHd = new MasPartHd();
        masPartHd.setPartCode(file.getName());
        masPartHd.setPartName(file.getName());
        List<MasPartRegion> masPartRegionList = new ArrayList<MasPartRegion>();
        if(/*true*/listfile!=null)
        {
            for (int i = 0; i < listfile.length; i++) {
                if (listfile[i].isDirectory()) {
                    if(listfile[i].getName().equals("PART")) {
                        File file2 = listfile[i];
                        File[] listfile2 = file2.listFiles();
                        for (int j = 0;j < listfile2.length; j++) {
                            if (listfile2[j].isDirectory()) {
                                masPartRegionList.add(getMasPartRegion(listfile2[j],imageType));
                            } else {
                                String temp = listfile2[j].toString().substring(7, listfile2[j].toString().length());
                                System.out.println("temp==" + temp);
                                System.out.println("文件"  + "---path=" + listfile2[j]);
                            }
                        }
                        //masPartRegionList.add(getMasPartRegion(listfile[i]));
                    }
                } else {
                    String temp = listfile[i].toString().substring(7, listfile[i].toString().length());
                    System.out.println("temp==" + temp);
                    System.out.println("文件"  + "---path=" + listfile[i]);
                }
            }
        }
        masPartHd.setMasPartRegions(masPartRegionList);
        return masPartHd;
    }

    /*
     * 获取 MasPartRegion
     */
    public MasPartRegion getMasPartRegion(File file,String imageType) {
        File[] listfile = file.listFiles();
        MasPartRegion masPartRegion = new MasPartRegion();
        masPartRegion.setRegionCode(file.getName());
        masPartRegion.setRegionName(file.getName());
        List<MasPartAssemply> masPartAssemplyList = new ArrayList<MasPartAssemply>();
        for (int i = 0; i < listfile.length; i++) {
            if (listfile[i].isDirectory()) {
                masPartAssemplyList.add(getMasPartAssemply(listfile[i],imageType));
            } else {
                String temp = listfile[i].toString().substring(7, listfile[i].toString().length());
                System.out.println("temp==" + temp);
                System.out.println("文件" + "---path=" + listfile[i]);
            }
        }
        masPartRegion.setMasPartAssemplys(masPartAssemplyList);
        return masPartRegion;
    }

    /*
     * 获取 MasPartAssemply
     */
    public MasPartAssemply getMasPartAssemply(File file,String imageType) {
        File[] listfile = file.listFiles();
        MasPartAssemply masPartAssemply = new MasPartAssemply();
        masPartAssemply.setAssemplyCode(file.getName());
        masPartAssemply.setAssemplyName(file.getName());
        List<MasPartFabric> masPartFabricList = new ArrayList<MasPartFabric>();
        for (int i = 0; i < listfile.length; i++) {
            MasPartFabric masPartFabric = new MasPartFabric();
            if (listfile[i].isDirectory()) {
                masPartFabric.setFabricName(listfile[i].getName());
                masPartFabric.setFabricCode(listfile[i].getName());
                masPartFabric.setFullFilePath(getMasPartFabric(listfile[i],imageType));
                masPartFabric.setSeq(i);
                masPartFabricList.add(masPartFabric);
            } else {
                if (listfile[i].getName().indexOf("Full") > 0) {
                    masPartFabric.setFabricName(listfile[i].getName());
                    masPartFabric.setFabricCode(listfile[i].getName());
                    masPartFabric.setFullFilePath(listfile[i].getName());
                    masPartFabricList.add(masPartFabric);
                }
            }
        }
        masPartAssemply.setMasPartFabrics(masPartFabricList);
        return masPartAssemply;
    }

    /*
     * 获取 MasPartFabic
     */
    public String getMasPartFabric(File file,String imageType) {
        File[] listfile = file.listFiles();
        String FullFilePath="";
        for (int i = 0; i < listfile.length; i++) {
            if (!listfile[i].isDirectory()) {
                if (listfile[i].getName().indexOf(imageType) > 0) {
                    // FullFilePath=listfile[i].getName();
                    FullFilePath= fullFilePath(listfile[i].toString());
                    break;
                }
            }
        }
        return FullFilePath;
    }

    public String fullFilePath(String path){
        String a=null;
        int pos;
        pos = path.indexOf("06.Upload")+"06.Upload".length()+1;
        String sub = path.substring(pos).replace("\\","/");
        return sub;
    }
}
